/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvTravail;
import fr.cggveritas.crewintranet.jms.entities.CvTypedecontrat;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvTypedecontratJpaController extends DaoImp implements Serializable {

    public CvTypedecontratJpaController() {
        super();
    }

    public void create(CvTypedecontrat cvTypedecontrat) throws PreexistingEntityException, Exception {
        if (cvTypedecontrat.getCvTravailList() == null) {
            cvTypedecontrat.setCvTravailList(new ArrayList<CvTravail>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<CvTravail> attachedCvTravailList = new ArrayList<CvTravail>();
            for (CvTravail cvTravailListCvTravailToAttach : cvTypedecontrat.getCvTravailList()) {
                cvTravailListCvTravailToAttach = em.getReference(cvTravailListCvTravailToAttach.getClass(), cvTravailListCvTravailToAttach.getCvTravailleurId());
                attachedCvTravailList.add(cvTravailListCvTravailToAttach);
            }
            cvTypedecontrat.setCvTravailList(attachedCvTravailList);
            em.persist(cvTypedecontrat);
            for (CvTravail cvTravailListCvTravail : cvTypedecontrat.getCvTravailList()) {
                CvTypedecontrat oldTypecontratOfCvTravailListCvTravail = cvTravailListCvTravail.getTypecontrat();
                cvTravailListCvTravail.setTypecontrat(cvTypedecontrat);
                cvTravailListCvTravail = em.merge(cvTravailListCvTravail);
                if (oldTypecontratOfCvTravailListCvTravail != null) {
                    oldTypecontratOfCvTravailListCvTravail.getCvTravailList().remove(cvTravailListCvTravail);
                    oldTypecontratOfCvTravailListCvTravail = em.merge(oldTypecontratOfCvTravailListCvTravail);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvTypedecontrat(cvTypedecontrat.getTypecontrat()) != null) {
                throw new PreexistingEntityException("CvTypedecontrat " + cvTypedecontrat + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvTypedecontrat cvTypedecontrat) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypedecontrat persistentCvTypedecontrat = em.find(CvTypedecontrat.class, cvTypedecontrat.getTypecontrat());
            List<CvTravail> cvTravailListOld = persistentCvTypedecontrat.getCvTravailList();
            List<CvTravail> cvTravailListNew = cvTypedecontrat.getCvTravailList();
            List<String> illegalOrphanMessages = null;
            for (CvTravail cvTravailListOldCvTravail : cvTravailListOld) {
                if (!cvTravailListNew.contains(cvTravailListOldCvTravail)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvTravail " + cvTravailListOldCvTravail + " since its typecontrat field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<CvTravail> attachedCvTravailListNew = new ArrayList<CvTravail>();
            for (CvTravail cvTravailListNewCvTravailToAttach : cvTravailListNew) {
                cvTravailListNewCvTravailToAttach = em.getReference(cvTravailListNewCvTravailToAttach.getClass(), cvTravailListNewCvTravailToAttach.getCvTravailleurId());
                attachedCvTravailListNew.add(cvTravailListNewCvTravailToAttach);
            }
            cvTravailListNew = attachedCvTravailListNew;
            cvTypedecontrat.setCvTravailList(cvTravailListNew);
            cvTypedecontrat = em.merge(cvTypedecontrat);
            for (CvTravail cvTravailListNewCvTravail : cvTravailListNew) {
                if (!cvTravailListOld.contains(cvTravailListNewCvTravail)) {
                    CvTypedecontrat oldTypecontratOfCvTravailListNewCvTravail = cvTravailListNewCvTravail.getTypecontrat();
                    cvTravailListNewCvTravail.setTypecontrat(cvTypedecontrat);
                    cvTravailListNewCvTravail = em.merge(cvTravailListNewCvTravail);
                    if (oldTypecontratOfCvTravailListNewCvTravail != null && !oldTypecontratOfCvTravailListNewCvTravail.equals(cvTypedecontrat)) {
                        oldTypecontratOfCvTravailListNewCvTravail.getCvTravailList().remove(cvTravailListNewCvTravail);
                        oldTypecontratOfCvTravailListNewCvTravail = em.merge(oldTypecontratOfCvTravailListNewCvTravail);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = cvTypedecontrat.getTypecontrat();
                if (findCvTypedecontrat(id) == null) {
                    throw new NonexistentEntityException("The cvTypedecontrat with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypedecontrat cvTypedecontrat;
            try {
                cvTypedecontrat = em.getReference(CvTypedecontrat.class, id);
                cvTypedecontrat.getTypecontrat();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvTypedecontrat with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CvTravail> cvTravailListOrphanCheck = cvTypedecontrat.getCvTravailList();
            for (CvTravail cvTravailListOrphanCheckCvTravail : cvTravailListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvTypedecontrat (" + cvTypedecontrat + ") cannot be destroyed since the CvTravail " + cvTravailListOrphanCheckCvTravail + " in its cvTravailList field has a non-nullable typecontrat field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(cvTypedecontrat);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvTypedecontrat> findCvTypedecontratEntities() {
        return findCvTypedecontratEntities(true, -1, -1);
    }
    public List<CvTypedecontrat> findCvTypedecontratEntitiesByActive(boolean etat) {
        EntityManager em = getEntityManager();
        try {
            
            List<CvTypedecontrat> listCvTypedecontrats = new ArrayList<CvTypedecontrat>();
            Query query = em.createNamedQuery("CvTypedecontrat.findByActive");
            List result = query.setParameter("active", etat).getResultList();
            if(!result.isEmpty()){
                return result;
            }
            return listCvTypedecontrats;
            
        } finally {
            em.close();
        }
    }

    public List<CvTypedecontrat> findCvTypedecontratEntities(int maxResults, int firstResult) {
        return findCvTypedecontratEntities(false, maxResults, firstResult);
    }

    private List<CvTypedecontrat> findCvTypedecontratEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvTypedecontrat.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvTypedecontrat findCvTypedecontrat(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvTypedecontrat.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvTypedecontratCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvTypedecontrat> rt = cq.from(CvTypedecontrat.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
